//index.js
//获取应用实例
const app = getApp()
Page({
	data: {
		userInfo: {},
		hasUserInfo: '',
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		phone: '',
	},
	//事件处理函数
	bindViewTap: function() {
		wx.navigateTo({
			url: '../logs/logs',
		})
	},
	onShow() {
		if (
			app.globalDataO.openid.objectSid == '' ||
			app.globalDataO.openid.objectSid == null
		) {
			console.log('未授权')
		} else {
			this.getPhone()
		}

		console.log(app.globalDataO.openid.objectSid)
	},
	onLoad: function(options) {
		var that = this
		console.log(app.globalDataO.openid)
		wx.getSetting({
			success: function(res) {
				if (res.authSetting['scope.userInfo']) {
					console.log('已经授权')
					// 已经授权，可以直接调用 getUserInfo 获取头像昵称
					wx.getUserInfo({
						success: function(res) {
							console.log(res.userInfo)
							app.globalData.userInfo = res.userInfo
							console.log(app.globalData.userInfo)
							that.setData({
								userInfo: res.userInfo,
								hasUserInfo: true,
							})
							// app.getUid()
						},
					})
				} else {
					console.log('这是什，额')
				}
			},
		})

		if (app.globalData.userInfo) {
			console.log('进来')
			app.getUid()
			this.setData({
				userInfo: app.globalData.userInfo,
				hasUserInfo: false,
			})
			console.log(app.globalData.userInfo)
		} else if (this.data.canIUse) {
			console.log('又进来')
			console.log(app.globalData.userInfo)
			// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
			// 所以此处加入 callback 以防止这种情况
			app.userInfoReadyCallback = res => {
				this.setData({
					userInfo: res.userInfo,
					hasUserInfo: true,
				})
			}
		} else {
			// 在没有 open-type=getUserInfo 版本的兼容处理
			console.log('还进来')
			wx.getUserInfo({
				success: res => {
					app.globalData.userInfo = res.userInfo
					this.setData({
						userInfo: res.userInfo,
						hasUserInfo: true,
					})
				},
			})
		}
	},
	getuserInfo1: function(e) {
		app.getOpenid()
		console.log(e)
		app.globalData.userInfo = e.detail.userInfo
		// this.setData({
		// 	userInfo: e.detail.userInfo,
		// 	hasUserInfo: true,
		// })
		this.onLoad()
	},
	// 获得绑定手机号码
	getPhone: function() {
		let _this = this
		app.gets(
			app.url.selectPhone +
				'?memberId=' +
				app.globalDataO.openid.objectSid,
		).then(re => {
			console.log(re.data)
			if (!re.data.member.phone) {
				return
			}
			_this.setData({
				phone: re.data.member.phone,
			})
		})
	},
	bind_phone() {
		let _this = this
		console.log(this.data.phone)
		if (this.data.phone == '') {
			wx.navigateTo({
				url: '../../pages/login/login',
			})
		} else {
			wx.navigateTo({
				url: '../../pages/phone/phone?phone=' + _this.data.phone,
			})
		}
	},
	//公告
	bind_announcements() {
		wx.navigateTo({
			url: '../../pages/announcements/announcements',
		})
	},
	about_us() {
		wx.navigateTo({
			url: '../../pages/about_us/about_us',
		})
	},
	membership_card() {
		wx.navigateTo({
			url: '../../pages/membership_card/membership_card',
			// 我的卡券
		})
	},
	bind_card() {
		wx.navigateTo({
			url: '../../pages/my_card/my_card',
		})
	},
	address() {
		wx.navigateTo({
			url: '../../pages/address_manage/address',
		})
	},
	problem_feedback() {
		wx.navigateTo({
			url: '../../pages/problem/problem',
		})
	},
	// 推广
	spread() {
		wx.navigateTo({
			url: '../../pages/spread/spread',
		})
	},
	sta(e) {
		var status = e.currentTarget.dataset.id
        var name = e.currentTarget.dataset.name
        let ordetType = {
            status:status,
            name:name,
        }
        wx.setStorageSync('ordetType', JSON.stringify(ordetType))
		// wx.navigateTo({
		//   url: '../../pages/orderlist/orderlist?status=' + status + '&name=' + name,
        // })
		wx.switchTab({
			url: '../../pages/orderlist/orderlist',
		})
	},

	order_tab() {
		wx.navigateTo({
			url: '../../pages/orderlist/orderlist',
		})
	},
})
